﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmallERP.DAL;
using SmallERP.MODEL.FoundationModel;
using DevExpress.XtraGrid;
using System.Data;

namespace SmallERP.Bll.BaseInfo
{
    public class Papertype:IDBAccess
    {

        /// <summary>
        ///  获得纸型列表
        /// </summary>
        /// <returns></returns>
        public List<PaperType_Info> GetPaperTypeList()
        {
            List<PaperType_Info> list = new List<PaperType_Info>();
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    List<PaperType> listPaperType = entities.PaperType.ToList();
            //    foreach (var item in listPaperType)
            //    {
            //        PaperType_Info paperTypeInfo = new PaperType_Info();
            //        paperTypeInfo.PaperTypeId = item.PaperTypeId;
            //        paperTypeInfo.PaperTypeName = item.PaperTypeName;
            //        list.Add(paperTypeInfo);
            //    }
            //}
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperType");
            for (int i=0;i<dt.Rows.Count;i++)
            {
                DataRow row=dt.Rows[i];
                PaperType_Info paperFace = new PaperType_Info();
                paperFace.PaperTypeId = Convert.ToInt32(row["PaperTypeId"]);
                paperFace.PaperTypeName =(row["PaperTypeName"]).ToString();
                list.Add(paperFace);
 
            }
            return list;
            
        }


        /// <summary>
        /// 获得纸型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PaperType_Info GetPaperType(string name)
        {
            //PaperType_Info paperTypeInfo = new PaperType_Info();
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    PaperType paperType = entities.PaperType.FirstOrDefault(p => p.PaperTypeName == name);
            //    if (paperType != null)
            //    {
            //        paperTypeInfo.PaperTypeId = paperType.PaperTypeId;
            //        paperTypeInfo.PaperTypeName = paperType.PaperTypeName;
            //    }
            //    else
            //    {
            //        paperTypeInfo = null;
            //    }
            //}
            //return paperTypeInfo;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperType where PaperTypeName='" + name+"'");

            DataRow row = dt.Rows[0];
            PaperType_Info papertypeInfo = new PaperType_Info();
            papertypeInfo.PaperTypeId = Convert.ToInt32(row["PaperTypeId"]);
            papertypeInfo.PaperTypeName = (row["PaperTypeName"]).ToString();


            return papertypeInfo;
        }

        #region IDBAccess 成员
        /// <summary>
        /// 添加纸型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Add(object o)
        {
            int ret = 0;
            //PaperType_Info paperTypeInfo = (PaperType_Info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperType pInDataBase = entities.PaperType.FirstOrDefault(p => p.PaperTypeName == paperTypeInfo.PaperTypeName);
            //        if (pInDataBase != null)
            //        {
            //            return -2;
            //        }
            //        PaperType paperType = new PaperType();
            //        paperType.PaperTypeName = paperTypeInfo.PaperTypeName;
            //        entities.AddToPaperType(paperType);
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            PaperType_Info info = (PaperType_Info)o;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperType where PaperTypeName='" + info.PaperTypeName+"'");
            if (dt.Rows.Count == 0)
            {
                try
                {
                    string Sql = "insert into PaperType (PaperTypeName) values('" + info.PaperTypeName + "')";
                    ret=DAL.AccessHelper.ExecuteNonQuery(Sql);
                    
                   
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;
          
        }
        /// <summary>
        /// 更新纸型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Update(object o)
        {
            int ret = 0;
            //PaperType_Info paperTypeInfo = (PaperType_Info)o;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperType pInDataBase = entities.PaperType.FirstOrDefault(p => p.PaperTypeName == paperTypeInfo.PaperTypeName);
            //        PaperType paperType = entities.PaperType.FirstOrDefault(p => p.PaperTypeId == paperTypeInfo.PaperTypeId);
            //        if (pInDataBase != null&&pInDataBase.PaperTypeId !=paperType.PaperTypeId)
            //        {
            //            return -2;
            //        }
                   
            //        paperType.PaperTypeName = paperTypeInfo.PaperTypeName;
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            //return ret;
            PaperType_Info info = (PaperType_Info)o;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperType where PaperTypeId=" + info.PaperTypeId + "");
            if (dt.Rows.Count != 0)
            {
                try
                {
                    string Sql = "update PaperType  set PaperTypeName='" + info.PaperTypeName + "' where PaperTypeId=" + info.PaperTypeId + "";
                    ret=DAL.AccessHelper.ExecuteNonQuery(Sql);
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;
        }
        /// <summary>
        /// 删除纸型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int Delete(object o)
        {
            int ret = 0;
            PaperType_Info info = (PaperType_Info)o;
            DataTable dt = DAL.AccessHelper.dataTable("select * from PaperType where PaperTypeId=" + info.PaperTypeId + "");
            if (dt.Rows.Count != 0)
            {
                try
                {
                    string Sql = "delete   from PaperType where PaperTypeId=" + info.PaperTypeId + "";
                    ret=DAL.AccessHelper.ExecuteNonQuery(Sql);
                }
                catch
                {
                    ret = -1;
                }
            }
            else
            {
                return -2;
            }
            return ret;
           
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int LinkGridControl(object o)
        {
            int ret = 0;
            try
            {
                ((GridControl)o).DataSource = GetPaperTypeList();
                ((GridControl)o).RefreshDataSource();
            }
            catch
            {
                ret = -1;
            }
            return ret;
        }

        #endregion
    }
}
